//
//  TravelsViewController.m
//  Tour
//
//  Created by 张张烨 on 16/3/7.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "TravelsViewController.h"
#import "DsErErViewController.h"
#import "DsErYiViewController.h"

static CGFloat const maxTitleScale = 1.3;


@interface TravelsViewController ()<UIScrollViewDelegate>
{
    MBProgressHUD *HUD;
}
@property (nonatomic, retain) BaseScrollView *titleScrollView;
@property (nonatomic, retain) BaseScrollView *contentScrollView;
// 选中按钮
@property (nonatomic, retain) BaseButton *selTitleButton;
@property (nonatomic, retain) NSMutableArray *buttons;

@end

@implementation TravelsViewController

/**<  懒加载,我就试试 */
- (NSMutableArray *)buttons
{
    if (!_buttons)
    {
        self.buttons = [NSMutableArray array];
    }
    return _buttons;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationItem.title = @"游记";
    
    [self setupTitleScrollView];//创建小scrollview
    [self setupContentScrollView];//创建大scrollview
    
    [self addChildViewController];//添加子控制器
    [self setupTitle];//根据子视图个数创建小scrollview的按钮
    
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.contentScrollView.contentSize = CGSizeMake(self.childViewControllers.count * WIDTH, 0);
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.delegate = self;
    self.contentScrollView.userInteractionEnabled = YES;
    
    [self showTextDialog];
}


#pragma mark - 小菊花实现
- (void)showTextDialog
{
    //初始化进度框，置于当前的View当中
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    
    //如果设置此属性则当前的view置于后台
    HUD.dimBackground = YES;
    
    //设置对话框文字
    HUD.labelText = @"请稍等";
    
    //显示对话框
    [HUD showAnimated:YES whileExecutingBlock:^{
        //对话框显示时需要执行的操作
        sleep(5);
    } completionBlock:^{
        //操作执行完后取消对话框
        [HUD removeFromSuperview];
        
        HUD = nil;
    }];
}






#pragma mark - 设置头部标题栏
- (void)setupTitleScrollView
{
    // 判断是否存在导航控制器来判断y值
    CGFloat y = self.navigationController ? 64 : 0;
    CGRect rect = CGRectMake(0, 0, WIDTH, 44 * GAO);
//    CGFloat y = self.navigationController ? 64 * GAO : 0;
//    CGRect rect = CGRectMake(0,  GAO, WIDTH, 44 * GAO);
    
    BaseScrollView *titleScrollView = [[BaseScrollView alloc] initWithFrame:rect];
    titleScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:titleScrollView];
    
    self.titleScrollView = titleScrollView;
}

#pragma mark - 设置内容
- (void)setupContentScrollView
{
    CGFloat y = CGRectGetMaxY(self.titleScrollView.frame);
    CGRect rect = CGRectMake(0, y, WIDTH, HEIGH - 64*GAO);
    
    BaseScrollView *contentScrollView = [[BaseScrollView alloc] initWithFrame:rect];
    contentScrollView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:contentScrollView];
    
    self.contentScrollView = contentScrollView;
}
#pragma mark - 添加子控制器
- (void)addChildViewController
{
    DsErYiViewController *vc = [[DsErYiViewController alloc] init];
    vc.title = @"游记";
    [self addChildViewController:vc];
    
    DsErErViewController *vc1= [[DsErErViewController alloc]init];
    vc1.title = @"视频";
    [self addChildViewController:vc1];
}

#pragma mark - 设置标题
/**<  根据宽度换几个 */
- (void)setupTitle
{
    NSUInteger count = self.childViewControllers.count;
    
    CGFloat x = 0;
    CGFloat w = WIDTH / 2;
    CGFloat h = 44*GAO;
    
    for (int i = 0; i < count; i++)
    {
        UIViewController *vc = self.childViewControllers[i];
        
        x = i * w;
        CGRect rect = CGRectMake(x, 0, w, h);
        BaseButton *btn = [[BaseButton alloc] initWithFrame:rect];
        
        btn.tag = i;
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        
        [btn addTarget:self action:@selector(chick:) forControlEvents:UIControlEventTouchDown];
        
        [self.buttons addObject:btn];
        [self.titleScrollView addSubview:btn];
        
        if (i == 0)
        {
            [self chick:btn];
        }
        
    }
    self.titleScrollView.contentSize = CGSizeMake(count * w, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
}
// 按钮点击
- (void)chick:(BaseButton *)btn
{
    [self selTitleBtn:btn];
    
    NSUInteger i = btn.tag;
    CGFloat x = i * WIDTH;
    
    [self setUpOneChildViewController:i];
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
    
}

// 选中按钮
- (void)selTitleBtn:(BaseButton *)btn
{
    [self.selTitleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.selTitleButton.transform = CGAffineTransformIdentity;
    
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn.transform = CGAffineTransformMakeScale(maxTitleScale, maxTitleScale);
    
    self.selTitleButton = btn;
    [self setupTitleCenter:btn];
}
- (void)setUpOneChildViewController:(NSUInteger)i
{
    CGFloat x = i * WIDTH;
    
    UIViewController *vc = self.childViewControllers[i];
    
    if (vc.view.superview) {
        return;
    }
    vc.view.frame = CGRectMake(x, 0, WIDTH, HEIGH - self.contentScrollView.frame.origin.y);
    
    [self.contentScrollView addSubview:vc.view];
    
}

- (void)setupTitleCenter:(UIButton *)btn
{
    CGFloat offset = btn.center.x - WIDTH * 0.5;
    
    if (offset < 0)
    {
        offset = 0;
    }
    
    CGFloat maxOffset = self.titleScrollView.contentSize.width - WIDTH;
    if (offset > maxOffset)
    {
        offset = maxOffset;
    }
    
    [self.titleScrollView setContentOffset:CGPointMake(offset, 0) animated:YES];
    
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSUInteger i = self.contentScrollView.contentOffset.x / WIDTH;
    [self selTitleBtn:self.buttons[i]];
    [self setUpOneChildViewController:i];
}

// 只要滚动UIScrollView就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger leftIndex = offsetX / WIDTH;
    NSInteger rightIndex = leftIndex + 1;
    
    //    NSLog(@"%zd,%zd",leftIndex,rightIndex);
    
    BaseButton *leftButton = self.buttons[leftIndex];
    
    BaseButton *rightButton = nil;
    if (rightIndex < self.buttons.count) {
        rightButton = self.buttons[rightIndex];
    }
    
    CGFloat scaleR = offsetX / WIDTH - leftIndex;
    
    CGFloat scaleL = 1 - scaleR;
    
    
    CGFloat transScale = maxTitleScale - 1;
    leftButton.transform = CGAffineTransformMakeScale(scaleL * transScale + 1, scaleL * transScale + 1);
    
    rightButton.transform = CGAffineTransformMakeScale(scaleR * transScale + 1, scaleR * transScale + 1);
    
    
    UIColor *rightColor = [UIColor colorWithRed:scaleR green:0 blue:0 alpha:1];
    UIColor *leftColor = [UIColor colorWithRed:scaleL green:0 blue:0 alpha:1];
    
    [leftButton setTitleColor:leftColor forState:UIControlStateNormal];
    [rightButton setTitleColor:rightColor forState:UIControlStateNormal];
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
